package com.neu.niuyuchao.servlet;

import com.neu.niuyuchao.service.StuInfoService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.*;

@WebServlet(name = "AllagreeServlet", urlPatterns = {"/AllagreeServlet.do"})
public class AllagreeServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        String[] key=request.getParameterValues("approveBox");


        String[]k=new String[key.length];
        for (int i = 0; i <key.length ; i++) {

           String  ke= key[i].substring(key[i].lastIndexOf(",")+1);
           k[i]=ke;
        }

        List<Map<String,String>> list=new ArrayList();
        for(int i=0;i<key.length;i++){
            String[]shuju=key[i].split(",");
            Map<String,String>map=new HashMap<>();
            map.put("dor_build",shuju[0]);
            map.put("dor_no",shuju[1]);
            map.put("stu_id",shuju[2]);
            map.put("appro_id",shuju[3]);
            list.add(map);
        }
        Object[]b=new String[list.size()];
        int i=0;
        for (Map<String,String>map:list){
          Object o= map.get("stu_id");
          b[i]=o;
          i++;
        }

        StuInfoService s=new StuInfoService();
        boolean flag=s.Allagree(k);
        Object[] param= s.insert(list);
//        System.out.println(param);


          int row= s.UpdateStu(param,b);
            if (row>0){
                System.out.println("成功");
            }else {
                System.out.println("失败 ");
            }



        if (flag){
            response.sendRedirect(request.getContextPath()+"/commons/success.jsp");
        }else {
            response.sendRedirect(request.getContextPath()+"/commons/fail.jsp");
        }


    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
